package net.lahuobao.mall.service.impl.admin;

import net.lahuobao.mall.entity.admin.MallEtcApplyEntity;
import net.lahuobao.mall.entity.admin.MallEtcCardRuleEntity;
import net.lahuobao.mall.entity.admin.MallEtcUserEntity;
import net.lahuobao.mall.exception.CarNoUsedException;
import net.lahuobao.mall.service.admin.MallEtcApplyServiceI;
import net.lahuobao.mall.service.admin.MallEtcCardRuleServiceI;
import net.lahuobao.mall.service.admin.MallEtcUserServiceI;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("mallEtcUserService")
@Transactional
public class MallEtcUserServiceImpl extends CommonServiceImpl implements MallEtcUserServiceI {
    @Autowired
    private MallEtcCardRuleServiceI etcCardRuleService;

    @Autowired
    private MallEtcApplyServiceI mallEtcApplyService;
    @Override
    public void saveAndUpdateCardRule(MallEtcUserEntity mallEtcUser) throws CarNoUsedException {
        /*boolean isExists = isCarNoInSys(mallEtcUser.getCarno());
        if(!isExists){
            throw new CarNoUsedException("车牌号系统不存在，不能关联");
        }
        //验证是否已经领用
        boolean isUsed  = isCarNoUsed(mallEtcUser.getCarno());
        if(isUsed){
            throw new CarNoUsedException("该车牌号已申请过etc卡，不能重复关联");
        }*/

        boolean isBind = isBind(mallEtcUser.getCarno());
        if(isBind){
            throw new CarNoUsedException("该车牌号已经绑定ETC卡！");
        }
        boolean isApply = isApply(mallEtcUser.getCarno());
        if(isApply){
            throw new CarNoUsedException("该车牌号正在申请ETC卡或者已经申请过ETC卡！");
        }
        MallEtcCardRuleEntity etcCardRuleEntity = etcCardRuleService.singleResult("from MallEtcCardRuleEntity where cardno='"+mallEtcUser.getCardno()+"'");
        //已经使用
        etcCardRuleEntity.setState(2);
        etcCardRuleService.saveOrUpdate(etcCardRuleEntity);
        super.save(mallEtcUser);
    }

    /**
     * 查询车牌号是否已申请过etc卡
     * @param carNo 车牌号
     * @return 车牌号是否已申请过etc卡
     */
    public boolean isCarNoUsed(String carNo) {
        boolean isUsed = false;
        //申请信息 state 状态,已申请未支付_0,已支付未受理_1,待发卡_2,待收货_3,已收货_4,商户审核未通过_10,已取消_11
        List<MallEtcApplyEntity> mallEtcApply = super.findHql("from MallEtcApplyEntity where carno=? and state in(0,1,2,3)", carNo);

        if(mallEtcApply!=null&&mallEtcApply.size()>0){
            isUsed = true;
        }
        if(!isUsed){
            /**state 状态，0未使用,1已分配，待审核，2,已使用*/
            List<MallEtcCardRuleEntity> existCardRule = super.findHql("from MallEtcCardRuleEntity a where exists (select 1 from MallEtcUserEntity b where a.cardno=b.cardno and b.carno=?) and a.state in (1,2)", carNo);
            isUsed = existCardRule!=null&&existCardRule.size()>0;
        }
        return isUsed;
    }

    /**
     * 查询系统车牌号是否存在
     * @param carNo 车牌号
     * @return 系统车牌号是否存在
     */
    public boolean isCarNoInSys(String carNo) {
        Long vehicleCount = super.getCountForJdbc("select count(1) from lahuobao.vehicle where vehicle_no='"+carNo+"'");
        return vehicleCount > 0;
    }

    /**
     * 是否已经绑定
     * @param carNo
     * @return
     */
    private boolean isBind(String carNo){
        boolean isBind = false;
        List<MallEtcUserEntity> etcUserLists = super.findByProperty(MallEtcUserEntity.class, "carno", carNo);
        if (etcUserLists.size() > 0) {
            isBind = true;
        }
        return isBind;
    }

    /**
     * 是否已经申请
     * @param carNo
     * @return
     */
    private boolean isApply(String carNo){
        boolean isApply = false;
        List<MallEtcApplyEntity> lists = mallEtcApplyService.findByProperty(MallEtcApplyEntity.class, "carno", carNo);
        if (lists.size() > 0) {
            for (MallEtcApplyEntity temp : lists) {
                if (temp.getState().intValue() == 1 || temp.getState().intValue() == 2 || temp.getState().intValue() == 3 || temp.getState().intValue() == 4) {
                    isApply = true;
                    break;
                }
            }
        }
        return isApply;
    }

}